// pages/guide/index.js
import { request } from "../../request1/index.js"
Page({
  data: {
    partsList: [],
    baseUrl: 'https://guide.hpaitech.cn/api/v11',
    queryParams: {
      page: 1,
      size: 10,
      name: ''
    },
    total: 0,
    loading: false
  },

  onLoad() {
    this.getPartList()
  },

  // 搜索输入
  onSearchInput(e) {
    this.setData({
      'queryParams.name': e.detail.value
    })
  },

  // 搜索按钮点击
  handleSearch() {
    this.setData({
      'queryParams.page': 1
    })
    this.getPartList()
  },

  // 获取配件列表
  async getPartList() {
    this.setData({ loading: true })
    request({ urlLeft: "/getPartList", method: "POST" }).then(result => {
        this.setData({
          partsList: result.data.data
        })
        this.setData({ loading: false })
        console.log(result.data.data)
    });
  },

  inputChange: function (e) {
    this.setData({
      keyword: e.detail.value,
    });
  },

  search: function () {
    const message = this.data.keyword; // 输入框中的关键词

    if (message=='') {
      wx.showToast({
          title: '请输入搜索内容',
          icon: 'none',
      });
    }else{
      request({ urlLeft: "/getPartList", data: { message }, method: "POST" })
      .then(res => {
          //console.log(res);
          
          let mypartsList = res.data.data;
          this.setData({
            partsList: mypartsList,
          })
      })
    }
    // 这里可以添加具体的搜索逻辑和请求后端的代码
  },


  // 下拉刷新
  onPullDownRefresh() {
    this.setData({
      'queryParams.page': 1
    })
    this.getPartList().then(() => {
      wx.stopPullDownRefresh()
    })
  },

  // 上拉加载更多
  onReachBottom() {
    if (this.data.partsList.length >= this.data.total) {
      return
    }
    this.setData({
      'queryParams.page': this.data.queryParams.page + 1
    })
    this.getPartList()
  }
})